package com.shop.service.impl;

import com.shop.dao.CartDao;
import com.shop.dao.ProductDao;
import com.shop.entity.Cart;
import com.shop.entity.CartMaster;
import com.shop.entity.Product;
import com.shop.entity.Result;
import com.shop.service.CartService;
import com.shop.util.ResultUtil;
import com.shop.util.UUIDUtil;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.List;

/**
 * description: 购物车
 *
 * @author zhaoxiaozhao（xiaozhao.zhao@ucarinc.com）
 * @version 1.0
 * @date 2018/10/18 9:36
 **/
@Service
public class CartServiceImp implements CartService {
    @Resource
    private CartDao cartDao;
    @Resource
    private ProductDao productDao;
    /**
     * description:<通过用户id查找用户的购物车>
     *
     * @author zhaoxiaozhao（xiaozhao.zhao@ucarinc.com）
     * @date   2018/10/18 9:36
     * @param   buyerId 用户id
     * @return 
     **/
    @Override
    public CartMaster showCart(String buyerId) {
        //1.校验用户id是否为空，如果为空就不用查询数据库了
        if(StringUtils.isEmpty(buyerId)){
            return null;
        }
        //2.通过用户id查询用户的购物车
        List<Cart> cartList = null;
        try {
            cartList = cartDao.findCartByBuyerId(buyerId);
        } catch (Exception e) {
            return null;
        }
        if(cartList == null){
              return null;
        }
        //将cart集合封装在cartmaster
        CartMaster cartMaster = setCartMaster(cartList);
        return cartMaster;
    }

    /**
     * description:<通过用户id和商品id将商品从购物车移除>
     *
     * @author zhaoxiaozhao（xiaozhao.zhao@ucarinc.com）
     * @date   2018/10/18 14:30
     * @param  cart 携带着会员id和商品id
     * @return 
     **/
    @Override
    public Result deleteCart(Cart cart) {
        try {
            cartDao.deleteCart(cart);
        } catch (Exception e) {
            return ResultUtil.error("系统出错，删除购物车失败，请联系管理员");
        }
        return ResultUtil.success("删除购物车成功");
    }

    /**
     * description:<添加购物车>
     *
     * @author zhaoxiaozhao（xiaozhao.zhao@ucarinc.com）
     * @date   2018/10/19 15:22
     * @param  cart 有商品id和商品数量
     * @return 
     **/
    @Override
    public Result addCart(Cart cart) {
        //1.设置主键
        cart.setCartId(UUIDUtil.uuid());

        try {
            //2.检查是否有该商品
            Product product = productDao.findProductOne(cart.getProductId());
            if(null == product){
                 return ResultUtil.error("该商品不存在，添加购物车失败");
            }
            //3.添加商品到购物车
            cartDao.addCart(cart);
        } catch (Exception e) {
            return ResultUtil.error("添加商品到购物车失败，请稍后重试");
        }
        return ResultUtil.success("添加购物车成功");
    }

    /**
     * description:<修改购物车商品的数量>
     *
     * @author zhaoxiaozhao（xiaozhao.zhao@ucarinc.com）
     * @date   2018/10/19 16:47
     * @param  cart 有购物车id和商品数量
     * @return 
     **/
    @Override
    public Result updateCartNumber(Cart cart) {
        //1.检查数据
        if(cart == null && cart.getBuyerNumber() <= 0){
            return ResultUtil.error("数据错误，请刷新重试");
        }
        try {
            //2.修改数据
            cartDao.updateCartNumber(cart);
        } catch (Exception e) {
            return ResultUtil.error("");
        }
        return ResultUtil.success();
    }

    /**
     * description:<封装着购物车>
     *
     * @author zhaoxiaozhao（xiaozhao.zhao@ucarinc.com）
     * @date   2018/10/18 10:12
     * @param  cartList 购物车列表
     * @return 
     **/
    private CartMaster setCartMaster(List<Cart> cartList){
        CartMaster cartMaster = new CartMaster();
        cartMaster.setCartList(cartList);
        cartMaster.setCartTotalNumber(cartList.size());
        BigDecimal totalPrice = new BigDecimal(0);
        for (Cart cart: cartList ) {
            totalPrice.add(cart.getCartPrice());
        }
        cartMaster.setCarTotalPrice(totalPrice);
        return cartMaster;
    }
}
